<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>Pipeline &mdash; PowerShell fuer Systemintegratoren</title>
    
    <link rel="stylesheet" href="../_static/sphinxdoc.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '1.0',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/translations.js"></script>
    <link rel="top" title="PowerShell fuer Systemintegratoren" href="../index.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="Allgemeiner Index"
             accesskey="I">Index</a></li>
        <li><a href="../index.html">PS4SYS</a> &raquo;</li> 
      </ul>
    </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
            <p class="logo"><a href="../index.html">
              <img class="logo" src="../_static/PowerShell_icon_small.png" alt="Logo"/>
            </a></p>
  <h3>Diese Seite</h3>
  <ul class="this-page-menu">
    <li><a href="../_sources/loesung_rst/Loesung_PowerShell_Pipeline.txt"
           rel="nofollow">Quelltext anzeigen</a></li>
  </ul>
<div id="searchbox" style="display: none">
  <h3>Schnellsuche</h3>
    <form class="search" action="../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Los" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Geben Sie Suchbegriffe oder einen Modul-, Klassen- oder Funktionsnamen ein.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="pipeline">
<h1>Pipeline<a class="headerlink" href="#pipeline" title="Permalink zu dieser Überschrift">¶</a></h1>
<ul id="loesung-pipeline">
<li><p class="first">Virtual Memory</p>
<div class="highlight-sh"><div class="highlight"><pre>Get-Process | Sort-Object WorkingSet | Select-Object -First 5
</pre></div>
</div>
</li>
<li><p class="first">Largest File</p>
<div class="highlight-sh"><div class="highlight"><pre>Get-ChildItem -Recurse | Where-Object <span class="o">{</span> <span class="nv">$_</span>.Length -gt 10MB <span class="o">}</span> |
Sort-Object Length -Descending | Select-Object -First 10
</pre></div>
</div>
</li>
<li><p class="first">Foreach: Foreach-Objekt besrbeitet die Elemente der Pipeline sofort</p>
<div class="highlight-sh"><div class="highlight"><pre><span class="c"># ForEach-Object lists each element in a pipeline:</span>
Dir C:<span class="se">\ </span>-recurse | ForEach-Object <span class="o">{</span> <span class="nv">$_</span>.name <span class="o">}</span>
</pre></div>
</div>
</li>
<li><p class="first">Alias:</p>
<blockquote>
<div><div class="highlight-sh"><div class="highlight"><pre>Get-Alias | where-object <span class="o">{</span> <span class="nv">$_</span>.definition -eq <span class="s2">&quot;get-process&quot;</span> <span class="o">}</span>
</pre></div>
</div>
</div></blockquote>
</li>
<li><p class="first">20 MByte-Prozesse</p>
<blockquote>
<div><div class="highlight-sh"><div class="highlight"><pre>get-process | where-object <span class="o">{</span><span class="nv">$_</span>.WorkingSet64 -gt 20MB <span class="o">}</span>
ps | ? <span class="o">{</span><span class="nv">$_</span>.ws -gt 20MB <span class="o">}</span>
</pre></div>
</div>
</div></blockquote>
</li>
<li><p class="first">Sortiert nach GrÃƒÂ¶ÃƒÅ¸ee</p>
<blockquote>
<div><div class="highlight-sh"><div class="highlight"><pre>ps | ? <span class="o">{</span><span class="nv">$_</span>.ws -gt 20MB <span class="o">}</span> | sort ws
</pre></div>
</div>
</div></blockquote>
</li>
<li><p class="first">Ausgabe Produktversion</p>
<blockquote>
<div><div class="highlight-sh"><div class="highlight"><pre>ps | <span class="k">select </span>company, name, product, productversion
</pre></div>
</div>
</div></blockquote>
</li>
<li><p class="first">Dateien</p>
<blockquote>
<div><div class="highlight-sh"><div class="highlight"><pre>Get-Childitem h:<span class="se">\d</span>aten -filter *.doc -r
| Where-Object <span class="o">{</span> <span class="nv">$_</span>.Length -gt 40000 <span class="o">}</span>
| Select-Object Name, Length
| Sort-Object Length
| <span class="nb">export</span>-csv p:<span class="se">\G</span>rosseWordDateien.csv -notype
</pre></div>
</div>
</div></blockquote>
</li>
<li><p class="first">Geben Sie alle Unterordner bis zu einer bestimmten Tiefe aus, ausgehend vom jeweils aktuellen Ordner</p>
</li>
</ul>
<blockquote>
<div><p>$Cred = New-Object System.Management.Automation.PSCredential &#8220;Administrator&#8221;, $Pw</p>
<div class="highlight-sh"><pre>gci -recurse | ? {$_.PSisContainer -eq $true} | where{
       if(($_.fullname.split("\")).count -le 4)
       {
               write-host $_.fullname
       }
}</pre>
</div>
</div></blockquote>
<ul>
<li><p class="first">Sie wollen prÃƒÂ¼fen, ob auf bestimmten Rechnern innerhalb ihres LANs Software bestimmter Hersteller vorhanden ist. Schreiben Sie ein entsprechendes Skript</p>
<div class="highlight-sh"><div class="highlight"><pre><span class="c">########################################</span>
<span class="c"># The PowerShell script inventories the installed software</span>
<span class="c"># of a producer on n computer systems</span>
<span class="c"># (C) Dr. Holger Schwichtenberg</span>
<span class="c">########################################</span>
<span class="nv">$Producer</span> <span class="o">=</span> <span class="s2">&quot;*Microsoft*&quot;</span>
<span class="nv">$EntryFilename</span> <span class="o">=</span> <span class="s2">&quot;computernames.txt&quot;</span>
<span class="nv">$OutputFilename</span> <span class="o">=</span> <span class="s2">&quot;Softwareinventory.csv&quot;</span>
<span class="nv">$pw</span> <span class="o">=</span> Read-Host -AsSecureString




<span class="c"># Import of computer names</span>
<span class="nv">$Computernames</span> <span class="o">=</span> Get-Content <span class="s2">&quot;computernames.txt&quot;</span>
<span class="nv">$Computernames</span> | foreach <span class="o">{</span>
       <span class="k">if</span> <span class="o">(</span>Ping<span class="o">(</span><span class="nv">$_</span><span class="o">))</span>
       <span class="o">{</span>
               Write-Host <span class="s2">&quot;Inventorize software for computer $_ ...&quot;</span>
               <span class="c"># Fetching of installed MSI packages on all computers</span>

               <span class="nv">$Cred</span> <span class="o">=</span> New-Object System.Management.Automation.PSCredential <span class="nv">$_</span><span class="se">\A</span>dministrator, <span class="nv">$Pw</span>

               <span class="nv">$Software</span> <span class="o">=</span> foreach <span class="o">{</span> get-wmiobject win32_product -computername <span class="nv">$_</span> -credential <span class="nv">$Cred</span> <span class="o">}</span>
               | where <span class="o">{</span> <span class="nv">$_</span>.vendor -like <span class="nv">$Producer</span> <span class="o">}</span>
               <span class="c"># Export in CSV</span>
               <span class="nv">$Software</span> | <span class="nb">export</span>-csv <span class="nv">$OutputFileName</span> -notypeinformation
       <span class="o">}</span>
       <span class="k">else</span>
       <span class="o">{</span>
               Write-Error <span class="s2">&quot;Computer not accessible!&quot;</span>
       <span class="o">}</span>
<span class="o">}</span>
<span class="c"># Execute Ping</span>
<span class="k">function </span>Ping
<span class="o">{</span>
       <span class="nv">$status</span> <span class="o">=</span> Get-WmiObject Win32_PingStatus -Ã¯Â¬ï¿½lter <span class="s2">&quot;Address=&#39;$args[0]&#39;&quot;</span> |
       <span class="k">select </span>StatusCode
       <span class="k">return</span> <span class="nv">$status</span>.Statuscode -eq 0
<span class="o">}</span>
</pre></div>
</div>
</li>
</ul>
<p>Das Skript ist in mehrere Teile gegliedert:</p>
<ol class="arabic">
<li><p class="first">Function Ping</p>
<div class="highlight-sh"><div class="highlight"><pre><span class="c"># Execute Ping</span>
<span class="k">function </span>Ping
<span class="o">{</span>
       <span class="nv">$status</span> <span class="o">=</span> Get-WmiObject Win32_PingStatus -Ã¯Â¬ï¿½lter <span class="s2">&quot;Address=&#39;$args[0]&#39;&quot;</span> |
       <span class="k">select </span>StatusCode
       <span class="k">return</span> <span class="nv">$status</span>.Statuscode -eq 0
<span class="o">}</span>
</pre></div>
</div>
<p>ErhÃƒÂ¤lt als Parameter einen Computernamen, ermittelt den Statuscode und gibt true zurÃƒÂ¼ck, wenn der Computer erreichbar ist</p>
</li>
<li><p class="first">Einstieg in das Skript</p>
<blockquote>
<div><div class="highlight-sh"><div class="highlight"><pre><span class="nv">$Producer</span> <span class="o">=</span> <span class="s2">&quot;*Microsoft*&quot;</span>
<span class="nv">$Entry</span>Ã¯Â¬ï¿½lename <span class="o">=</span> <span class="s2">&quot;computernames.txt&quot;</span>
<span class="nv">$Output</span>Ã¯Â¬ï¿½lename <span class="o">=</span> <span class="s2">&quot;Softwareinventory.csv&quot;</span>
<span class="c"># Import of computer names</span>
<span class="nv">$Computernames</span> <span class="o">=</span> Get-Content <span class="s2">&quot;computernames.txt&quot;</span>
</pre></div>
</div>
<p>Definition von einigen Variablen
Lesen der zu ÃƒÂ¼berprÃƒÂ¼fenden Computernamen aus einer Textdatei</p>
</div></blockquote>
</li>
<li><p class="first">Informationsbeschaffung</p>
<div class="highlight-sh"><div class="highlight"><pre><span class="nv">$Computernames</span> | foreach <span class="o">{</span>
  <span class="k">if</span> <span class="o">(</span>Ping<span class="o">(</span><span class="nv">$_</span><span class="o">))</span>
  <span class="o">{</span>
        Write-Host <span class="s2">&quot;Inventorize software for computer $_ ...&quot;</span>
        <span class="c"># Fetching of installed MSI packages on all computers</span>
        <span class="nv">$Software</span> <span class="o">=</span> foreach <span class="o">{</span> get-wmiobject win32_product -computername <span class="nv">$_</span> <span class="o">}</span>
        | where <span class="o">{</span> <span class="nv">$_</span>.vendor -like <span class="nv">$Producer</span> <span class="o">}</span>
        <span class="c"># Export in CSV</span>
        <span class="nv">$Software</span> | <span class="nb">export</span>-csv <span class="s2">&quot;Softwareinventar.csv&quot;</span> -notypeinformation
  <span class="o">}</span>
  <span class="k">else</span>
  <span class="o">{</span>
        Write-Error <span class="s2">&quot;Computer not accessible!&quot;</span>
  <span class="o">}</span>
<span class="o">}</span>
</pre></div>
</div>
<p>ÃƒÅ“ber eine Pipeline werden die Computernamen nacheinander aufgerufen
Bei erfolgreichem Ping wird per WMI der Remote-Computer auf das
Vorhandensein des Herstellernamens abgefragt. Bei Vorhandensein wird es als
Ergebnis in eine CSV-Datei geschrieben.</p>
</li>
</ol>
<ul>
<li><p class="first">Erweitern Sie obiges Skript um die Abfrage nach bestimmten Softwarenamen bzw.
VersionsstÃƒÂ¤nden.</p>
<div class="highlight-sh"><div class="highlight"><pre><span class="k">function </span>Get-IsInstall<span class="o">(</span><span class="nv">$Application</span>, <span class="nv">$Computer</span>, <span class="nv">$Version</span><span class="o">)</span>
<span class="o">{</span>
       <span class="nv">$a</span> <span class="o">=</span> <span class="o">(</span>Get-WmiObject -Class Win32_Product -Filter
       <span class="s2">&quot;Name=&#39;$Application&#39; and Version=&#39;$Version&#39;&quot;</span> -computername
       <span class="nv">$Computer</span><span class="o">)</span>
       <span class="k">return</span> <span class="o">(</span><span class="nv">$a</span> -ne <span class="nv">$null</span><span class="o">)</span>
<span class="o">}</span>
<span class="nv">$e</span> <span class="o">=</span> Get-IsInstall <span class="s2">&quot;QuickTime&quot;</span> <span class="s2">&quot;E01&quot;</span> <span class="s2">&quot;7.2.0.240&quot;</span>
<span class="k">if</span> <span class="o">(</span><span class="nv">$e</span><span class="o">)</span>
<span class="o">{</span>
       <span class="s2">&quot;Software is installed!&quot;</span>
<span class="o">}</span>
<span class="k">else</span>
<span class="o">{</span>
       <span class="s2">&quot;Software is not installed!&quot;</span>
<span class="o">}</span>
</pre></div>
</div>
</li>
</ul>
<ul class="simple" id="index-0">
<li>I needed to merge 365 CSV files that represent daily weather data sets into one CSV file that contains all the data accumulated during one year. Each of the daily CSV files had a header row. The yearly file should only have one. Filtering out rows is a perfect application of the PowerShell filter functions. Übersetzen Sie den anglischen Text sinngemäß¸ und schreiben Sie ein entsprechendes Skript.</li>
</ul>
<p>This is where PowerShell&#8217;s pipeline programming is shining. It only takes a Filter function definition, inserting it into an pre-existing pipeline and you are done.</p>
<p>Here is the sample script:</p>
<p><a class="reference external" href="http://www.tellingmachine.com/post/Using-PowerShell-Filter-functions-to-filter-out-header-rows-CSV-file-merges.aspx">http://www.tellingmachine.com/post/Using-PowerShell-Filter-functions-to-filter-out-header-rows-CSV-file-merges.aspx</a></p>
<div class="highlight-sh"><div class="highlight"><pre><span class="c">#Filter Function filters out records that start with &quot;Time&quot;</span>
Filter Filter-Header
<span class="o">{</span>
        <span class="k">if</span><span class="o">(</span><span class="nv">$_</span> -match <span class="s2">&quot;^Time&quot;</span><span class="o">)</span>
        <span class="o">{</span>
                <span class="nv">$_</span> | out-null
        <span class="o">}</span>
        <span class="k">else</span>
        <span class="o">{</span>
                <span class="nv">$_</span>
        <span class="o">}</span>
<span class="o">}</span>

<span class="nb">cd</span> <span class="s2">&quot;C:\WeatherStation\FilterTest&quot;</span>

<span class="c">#Initializing yearly file</span>
<span class="nv">$YearlyFileName</span> <span class="o">=</span> <span class="s2">&quot;Weather2009.csv&quot;</span>
<span class="c">#Set the header only once in the yearly file</span>
Set-Content -Path <span class="nv">$YearlyFileName</span> -value <span class="s2">&quot;Time,TemperatureF,DewpointF,PressureIn,WindDirectionDegrees,WindSpeedMPH,WindSpeedGustMPH,Humidity,HourlyPrecipIn&quot;</span> -force -encoding <span class="s2">&quot;UTF8&quot;</span>

<span class="c">#Open all csv files get-content and add it to the yearly file</span>
<span class="nv">$Files</span> <span class="o">=</span> dir -Path <span class="s2">&quot;C:\Users\Klaus\Desktop\TO PLANET\WeatherStation\FilterTest&quot;</span> -Filter <span class="s2">&quot;*.csv&quot;</span>

<span class="c">#Filter in Action</span>
<span class="nv">$Files</span> | ForEach-Object <span class="sb">`</span>
<span class="o">{</span>
        Get-Content -Path <span class="nv">$_</span>.Name -Encoding <span class="s2">&quot;UTF8&quot;</span> | Filter-Header | Add-Content -path <span class="nv">$YearlyFileName</span> -Encoding <span class="s2">&quot;UTF8&quot;</span>
<span class="o">}</span>
</pre></div>
</div>
<ul>
<li><p class="first">Suchen Sie die letzten 2000 Systemereignisse des Eventlogs, die vom Typ &#8220;Error&#8221; sind</p>
<blockquote>
<div><div class="highlight-sh"><div class="highlight"><pre><span class="c"># PowerShell script to find Error messages in the System eventlog.</span>
Get-EventLog system -newest 2000 | where <span class="o">{</span><span class="nv">$_</span>.entryType -match <span class="s2">&quot;Error&quot;</span><span class="o">}</span>
</pre></div>
</div>
</div></blockquote>
</li>
<li><p class="first">Sie schreiben ein Powershell-Skript, welches eine eigene Ereignisquelle im Eventlog schafft und darunter seine Ereignisse schreibt.</p>
<div class="highlight-sh"><div class="highlight"><pre><span class="c">## Step 1 - PowerShell Script to create eventlog source by David Stone</span>
Clear-Host
<span class="k">if</span> <span class="o">(</span>!<span class="o">(</span><span class="nb">test</span>-path <span class="sb">`</span> HKLM:<span class="se">\S</span>YSTEM<span class="se">\C</span>urrentControlSet<span class="se">\S</span>ervices<span class="se">\E</span>ventlog<span class="se">\A</span>pplication<span class="se">\S</span>ampleApp <span class="o">))</span> <span class="sb">`</span>
<span class="o">{</span>new-eventlog -logname Application -source SampleApp <span class="sb">`</span>
-ErrorAction SilentlyContinue<span class="o">}</span>

<span class="c">## Step 2 - Create a &#39;Job start&#39; entry in your event log</span>
<span class="nv">$startTime</span> <span class="o">=</span> Get-date
<span class="nv">$startLog</span> <span class="o">=</span> <span class="s1">&#39;Sample job started at &#39;</span> +<span class="nv">$startTime</span>+ <span class="s1">&#39; local time&#39;</span>
write-eventlog -LogName Application -Message <span class="nv">$startLog</span> -Source SampleApp <span class="sb">`</span>
-id 1 -entrytype Information -Category 0

<span class="c">## Step 3 - A production script would have a payload here.</span>

<span class="c">## Step 4 - Write errors during processing (typically part of a if statement)</span>
write-eventLog -LogName Application -Message <span class="s1">&#39;Message content&#39;</span> <span class="sb">`</span>
-Source SampleApp -id 100 -entrytype Warning -category 0

<span class="c">## Step 5 - Write end of process entry in your event log</span>
<span class="nv">$endTime</span> <span class="o">=</span> Get-date
<span class="nv">$endLog</span> <span class="o">=</span> <span class="s1">&#39;Sample job ended at &#39;</span> +<span class="nv">$endTime</span>+ <span class="s1">&#39; local time&#39;</span>
write-eventlog -LogName Application -Message <span class="nv">$endLog</span> -Source SampleApp <span class="sb">`</span>
-id 9 -entrytype Information -category 0
</pre></div>
</div>
</li>
<li><p class="first">Schreiben Sie die Einträge der obigen Quelle in eine CSV-Datei. Es soll nur die jeweils letzte Stunde eingetragen werden</p>
<blockquote>
<div><div class="highlight-sh"><div class="highlight"><pre><span class="c">## Step 6 - Write the entries to a csv file</span>
Clear-Host
<span class="nv">$logtime</span><span class="o">=[</span>DateTime<span class="o">]</span>::Now.AddHours<span class="o">(</span>-1<span class="o">)</span>
<span class="nv">$CSVPath</span> <span class="o">=</span> <span class="s2">&quot;C:\SSource.csv&quot;</span>
Get-EventLog -LogName application -Source <span class="s1">&#39;SampleApp&#39;</span> <span class="sb">`</span>
-EntryType warning -After <span class="nv">$logtime</span> <span class="sb">`</span>
| <span class="k">select </span>eventid, machinename, entrytype, <span class="nb">source</span>, message, timegenerated <span class="sb">`</span>
| Export-Csv <span class="nv">$CSVPath</span> -NoTypeInformation
</pre></div>
</div>
</div></blockquote>
</li>
<li><p class="first">A Real-life Example of Select-String</p>
<p>My practical problem was that I wanted to search for instances a particular string in a directory with hundreds of file.  If a file had duplicate entries, then I needed to know.</p>
<p>The PowerShell problem is how to create a stream of the files, then compare each file with my string value.  In the output I needed to know the filename.</p>
<p>To solve the problem, I employed four main commands, which you can see in the following script:</p>
<div class="highlight-sh"><div class="highlight"><pre>    Get-ChildItem - recurse
    foreach <span class="o">(</span>loop<span class="o">)</span> <span class="o">{</span>what to <span class="k">do </span>block<span class="o">}</span>
<span class="k">if</span> <span class="o">(</span><span class="nb">test</span><span class="o">)</span> <span class="o">{</span>output block<span class="o">}</span>
<span class="k">select</span>-String -pattern to match my string value.
</pre></div>
</div>
<div class="highlight-sh"><div class="highlight"><pre><span class="c"># A real-life example of PowerShell&#39;s Select-String</span>
<span class="nv">$i</span><span class="o">=</span>0
<span class="nv">$File</span> <span class="o">=</span> Get-ChildItem <span class="s2">&quot;C:\html&quot;</span> -include *.html -recurse
<span class="nv">$StringVid</span> <span class="o">=</span> <span class="s2">&quot;msbuild&quot;</span>
foreach <span class="o">(</span><span class="nv">$Entry</span> in <span class="nv">$File</span><span class="o">)</span> <span class="o">{</span>
   <span class="nv">$List</span> <span class="o">=</span> <span class="k">select</span>-string -pattern <span class="nv">$StringVid</span> <span class="nv">$Entry</span>
          <span class="k">if</span> <span class="o">(</span><span class="nv">$List</span>.LongLength -gt 1<span class="o">)</span> <span class="o">{</span>
          <span class="s2">&quot;{0,-8} {1,-4} {2,18}&quot;</span> -f
          <span class="s2">&quot;Files &quot;</span>, <span class="nv">$List</span>.LongLength, <span class="nv">$Entry</span>.FullName;
          <span class="nv">$i</span>++
          <span class="o">}</span>
<span class="o">}</span>
</pre></div>
</div>
</li>
<li><p class="first">Namensauflösung einer IP-Range</p>
<p>Die Programmfunktionalität wird mit Hilfe von 2 Funktionen realisiert.
<strong>Get-ComputerNameByIP</strong> zeigt die Verwendung von Fehlerbehandlungscode,
<strong>Check-Online</strong> zeigt die Verwendung von Hintergrundprozessen.</p>
<p>Als Speicher für die IP-Adressen und Hostnamen wird die Datenstruktur eines <strong>Hashes</strong> verwendet,</p>
<div class="highlight-sh"><div class="highlight"><pre>&lt;<span class="c">#</span>
.SYNOPSIS
Gets a computer name
.DESCRIPTION
Resolve a computer name using an IP address
.PARAMETER &lt;paramName&gt;
<span class="nv">$IPAddress</span>
.EXAMPLE
Get-ComputerNameByIP <span class="s2">&quot;192.168.1.1&quot;</span>
.AUTHOR
G.W. Scheppink
<span class="c">#&gt;</span>

<span class="k">function </span>Get-ComputerNameByIP <span class="o">{</span>
                param<span class="o">(</span>
                        <span class="nv">$IPAddress</span> <span class="o">=</span> <span class="nv">$null</span>
                <span class="o">)</span>

                begin <span class="o">{</span> <span class="o">}</span>

                process <span class="o">{</span>
                        <span class="k">if</span> <span class="o">(</span><span class="nv">$IPAddress</span> -and <span class="nv">$_</span><span class="o">)</span> <span class="o">{</span>
                                Throw <span class="s2">&quot;Please use either pipeline or input parameter&quot;</span>
                                <span class="nb">break</span>
                        <span class="o">}</span> elseif <span class="o">(</span><span class="nv">$IPAddress</span><span class="o">)</span> <span class="o">{</span>
                                <span class="o">([</span>System.Net.Dns<span class="o">]</span>::GetHostbyAddress<span class="o">(</span><span class="nv">$IPAddress</span><span class="o">))</span>
                        <span class="o">}</span> elseif <span class="o">(</span><span class="nv">$_</span><span class="o">)</span> <span class="o">{</span>
                                <span class="nb">trap</span> <span class="o">[</span>Exception<span class="o">]</span> <span class="o">{</span>
                                        write-warning <span class="nv">$_</span>.Exception.Message
                                        <span class="k">continue</span>;
                                <span class="o">}</span>
                                <span class="o">[</span>System.Net.Dns<span class="o">]</span>::GetHostbyAddress<span class="o">(</span><span class="nv">$_</span><span class="o">)</span>
                        <span class="o">}</span> <span class="k">else</span> <span class="o">{</span>
                                <span class="nv">$IPAddress</span> <span class="o">=</span> Read-Host <span class="s2">&quot;Please supply the IP Address&quot;</span>
                                <span class="o">[</span>System.Net.Dns<span class="o">]</span>::GetHostbyAddress<span class="o">(</span><span class="nv">$IPAddress</span><span class="o">)</span>
                        <span class="o">}</span>
                <span class="o">}</span>

                end <span class="o">{</span> <span class="o">}</span>

        <span class="o">}</span> <span class="c"># End function</span>

        <span class="k">function </span>Check-Online <span class="o">{</span>
                param<span class="o">(</span>
                        <span class="nv">$computername</span>
                <span class="o">)</span>

                <span class="nb">test</span>-connection -count 1 -ComputerName <span class="nv">$computername</span> -TimeToLive 5 -asJob |
                Wait-Job |
                Receive-Job |
                Where-Object <span class="o">{</span> <span class="nv">$_</span>.StatusCode -eq 0 <span class="o">}</span> |
                Select-Object -ExpandProperty Address StatusCode
        <span class="o">}</span>

        <span class="c"># This code pings an IP segment from 192.168.1.1 to 192.168.1.254 and returns only those IPs that respond.</span>
        CLS

        <span class="nv">$ip_name</span> <span class="o">=</span> @<span class="o">{}</span>
        <span class="nv">$Start</span> <span class="o">=</span> Get-Date
        <span class="nv">$ips</span> <span class="o">=</span> 1..254 | ForEach-Object <span class="o">{</span> <span class="s2">&quot;192.168.0.$_&quot;</span> <span class="o">}</span>
        <span class="nv">$online</span> <span class="o">=</span> Check-Online -computername <span class="nv">$ips</span>
        <span class="nv">$online</span>

        foreach <span class="o">(</span><span class="nv">$PC</span> in <span class="nv">$online</span><span class="o">)</span> <span class="o">{</span>
                <span class="nv">$pcname</span> <span class="o">=</span>  Get-ComputerNameByIP <span class="nv">$PC</span>
                <span class="nv">$ip_name</span>+<span class="o">=</span>@<span class="o">{</span><span class="nv">$PC</span><span class="o">=</span><span class="nv">$pcname</span>.HostName<span class="o">}</span>
        <span class="o">}</span>

        <span class="nv">$End</span> <span class="o">=</span> Get-Date
        Write-Host <span class="s2">&quot;`nStarted at: &quot;</span> <span class="nv">$Start</span>
        Write-Host <span class="s2">&quot;Ended at: &quot;</span> <span class="nv">$End</span>

        <span class="nv">$ip_name</span>

        <span class="c">#umstÃƒÂ¤ndliches Schreiben der key-value-Paare aus einem Hash heraus</span>
        <span class="nv">$ip_name</span>.GetEnumerator<span class="o">()</span> | Sort-Object Name |
        ForEach-Object <span class="o">{</span><span class="s2">&quot;{0} , {1}&quot;</span> -f <span class="nv">$_</span>.Name,<span class="nv">$_</span>.Value<span class="o">}</span> |
        Add-Content C:<span class="se">\t</span>emp<span class="se">\l</span>og.csv<span class="sb">`</span>
</pre></div>
</div>
</li>
<li><p class="first">Laptop oder Desktop</p>
<blockquote>
<div><p>Sie sind die IT-Leiter einer kleinen mittelstÃƒÂ¤ndischen Firma. Sie mÃƒÂ¼ssen Inventur machen und Sie brauchen dazu die Anzahl der zur Zeit sich im Netz befindlichen Laptop-und Desktop-Computer in ihrer Firma.</p>
<div class="highlight-sh"><div class="highlight"><pre>Following is the full script.

&lt;<span class="c">#</span>
.Synopsis

   helper <span class="k">function </span>used by Get-HardwareType

.EXAMPLE

   Example of how to use this cmdlet
<span class="c">#&gt;</span>
<span class="k">function </span>Is-Laptop <span class="o">{</span>

<span class="o">[</span>CmdletBinding<span class="o">()]</span>
<span class="o">[</span>OutputType<span class="o">([</span>boolean<span class="o">])]</span>

        param
        <span class="o">(</span>
        <span class="o">[</span>Parameter<span class="o">(</span><span class="nv">Mandatory</span><span class="o">=</span><span class="nv">$true</span><span class="o">)]</span>
        <span class="o">[</span>string<span class="o">]</span>        <span class="nv">$strHostName</span>
        <span class="o">)</span>

        <span class="nv">$blnResult</span> <span class="o">=</span> <span class="nv">$false</span>
        <span class="nv">$query</span> <span class="o">=</span> <span class="s2">&quot;select __SERVER,ChassisTypes from Win32_SystemEnclosure&quot;</span>


                <span class="nv">$objWMI</span> <span class="o">=</span> Get-WmiObject -ComputerName <span class="nv">$strHostName</span> -Query <span class="nv">$query</span> -ErrorAction Stop

                switch <span class="o">(</span><span class="nv">$objWMI</span>.ChassisTypes<span class="o">)</span> <span class="o">{</span>

                        9  <span class="o">{</span><span class="nv">$blnResult</span> <span class="o">=</span> <span class="nv">$true</span><span class="o">}</span> <span class="c"># Laptop</span>
                        10 <span class="o">{</span><span class="nv">$blnResult</span> <span class="o">=</span> <span class="nv">$true</span><span class="o">}</span> <span class="c"># Notebook</span>
                        12 <span class="o">{</span><span class="nv">$blnResult</span> <span class="o">=</span> <span class="nv">$true</span><span class="o">}</span> <span class="c"># Docking Station</span>
                        14 <span class="o">{</span><span class="nv">$blnResult</span> <span class="o">=</span> <span class="nv">$true</span><span class="o">}</span> <span class="c"># Sub Notebook</span>
                        default <span class="o">{}</span>
                  <span class="o">}</span>
                  <span class="k">return</span> <span class="nv">$blnResult</span>

<span class="o">}</span> <span class="c"># end function</span>

&lt;<span class="c">#</span>

.Synopsis
  <span class="k">function </span>to determine chassis <span class="nb">type </span>using a WMI query

.DESCRIPTION
   <span class="k">function </span>to determine chassis <span class="nb">type </span>using a WMI query

.EXAMPLE
   <span class="s2">&quot;pc01&quot;</span>,<span class="s2">&quot;pc02&quot;</span>,<span class="s2">&quot;pc03&quot;</span> | Get-HardwareType
<span class="c">#&gt;</span>
<span class="k">function </span>Get-HardwareType <span class="o">{</span>

<span class="o">[</span>CmdletBinding<span class="o">()]</span>
<span class="o">[</span>OutputType<span class="o">([</span>psobject<span class="o">])]</span>

Param
<span class="o">(</span>
<span class="o">[</span>Parameter<span class="o">(</span><span class="nv">Mandatory</span><span class="o">=</span><span class="nv">$true</span>,ValueFromPipeline<span class="o">=</span><span class="nv">$true</span><span class="o">)]</span>
<span class="nv">$strHostName</span>
<span class="o">)</span>

process <span class="o">{</span>

        try <span class="o">{</span>

                <span class="nv">$objHostName</span> <span class="o">=</span> <span class="o">[</span>system.net.dns<span class="o">]</span>::GetHostByName<span class="o">(</span><span class="nv">$strHostName</span><span class="o">)</span>

                <span class="nv">$query</span> <span class="o">=</span> <span class="s2">&quot;select __SERVER,ChassisTypes from Win32_SystemEnclosure&quot;</span>

                        <span class="k">if</span> <span class="o">(</span>Test-Connection -ComputerName <span class="nv">$objHostName</span>.HostName -count 1 -erroraction silentlycontinue<span class="o">)</span> <span class="o">{</span>

                                try <span class="o">{</span>

                                        <span class="nv">$objResult</span> <span class="o">=</span> New-Object -TypeName psobject -Property @<span class="o">{</span><span class="nv">HostName</span><span class="o">=</span><span class="nv">$objHostName</span>.HostName;IsLaptop<span class="o">=(</span>Is-Laptop -strHostName <span class="nv">$objHostName</span>.HostName<span class="o">)}</span>

                                        <span class="k">return</span> <span class="nv">$objResult</span>

                                <span class="o">}</span>

                                catch <span class="o">{</span>

                                        <span class="s2">&quot;Error trying to query $($objHostName.HostName)&quot;</span>

                                <span class="o">}</span>

                   <span class="o">}</span>

                   <span class="k">else</span> <span class="o">{</span>

                                write-host <span class="s2">&quot;error connecting to $($objHostName.HostName)&quot;</span>

                   <span class="o">}</span>

        <span class="o">}</span>

        catch <span class="o">{</span>

                write-host <span class="s2">&quot;Unable to resolve DNS address for $strHostName&quot;</span>

        <span class="o">}</span>

  <span class="o">}</span>

<span class="o">}</span> <span class="c"># end function</span>


<span class="nv">$laptopCount</span> <span class="o">=</span> 0
<span class="nv">$desktopCount</span> <span class="o">=</span> 0

<span class="nv">$searcher</span> <span class="o">=</span> new-object directoryservices.directorysearcher<span class="o">([</span>ADSI<span class="o">]</span><span class="s2">&quot;&quot;</span>,<span class="s2">&quot;(&amp;(objectcategory=computer)(!operatingsystem=*server*))&quot;</span><span class="o">)</span>

<span class="o">[</span>void<span class="o">]</span><span class="nv">$searcher</span>.PropertiesToLoad.Add<span class="o">(</span><span class="s2">&quot;cn&quot;</span><span class="o">)</span>
<span class="nv">$arrMachineName</span> <span class="o">=</span> <span class="nv">$searcher</span>.findall<span class="o">()</span> | %<span class="o">{</span><span class="nv">$_</span>.properties.cn<span class="o">}</span>

<span class="nv">$result</span> <span class="o">=</span> <span class="nv">$arrMachineName</span> | Get-HardwareType
<span class="nv">$result</span>

<span class="nv">$result</span> | ForEach-Object <span class="o">{</span><span class="k">if</span> <span class="o">(</span><span class="nv">$_</span>.islaptop<span class="o">)</span> <span class="o">{</span><span class="nv">$laptopCount</span>++<span class="o">}</span> <span class="k">else</span> <span class="o">{</span><span class="nv">$desktopCount</span>++<span class="o">}}</span>

<span class="s2">&quot;Laptop Total: $laptopCount&quot;</span>
<span class="s2">&quot;Desktop Total: $desktopCount&quot;</span>

<span class="c">## beim Lesen aus Textdatei</span>
<span class="c">#$result = import-csv C:\temp\laptop_desktop.ps1 | Group-Object {$_.chassis} -noelement</span>
<span class="c">#$result = import-csv C:\temp\laptop_desktop.ps1 | Group-Object -property chassis -noelement</span>

<span class="c">#$result</span>
</pre></div>
</div>
</div></blockquote>
</li>
</ul>
</div>


          </div>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="Allgemeiner Index"
             >Index</a></li>
        <li><a href="../index.html">PS4SYS</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
        &copy; Copyright 2010, STE.
      Mit <a href="http://sphinx-doc.org/">Sphinx</a> 1.2b1 erstellt.
    </div>
  </body>
</html>